//
//  DuoBaoHisVC.m
//  夺宝吧
//
//  Created by xiaocai on 15/7/20.
//  Copyright (c) 2015年 FangYuan. All rights reserved.
//

#import "DuoBaoHisVC.h"
#import "MJRefresh.h"
#import "RecoardHisCell.h"
#import "PdcDetailVC.h"
#import "NoDataView.h"
#import "DuoBaoHisTab.h"
@interface DuoBaoHisVC ()<UIScrollViewDelegate>
{
    //往期列表
    UITableView *_tableView;
    //滑动控件
    UIScrollView *_s;
    
    UIButton *allBtn;
    UIButton *goBtn;
    UIButton *rltBtn;
    NSArray *_btns;
    
    //跟随条
    UILabel *_followLab;
}
@end

@implementation DuoBaoHisVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    [self loadCustomNaviBar];
    //加载选项按钮
    [self loadSelectBtns];
    //加载子界面
    [self loadSubViews];

}
- (void)setIsMyRecordFlag:(BOOL)isMyRecordFlag
{
    _isMyRecordFlag = isMyRecordFlag;

}
//加载导航条
- (void)loadCustomNaviBar
{
    UIView *naviBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WYMWITH, 20+44)];
    naviBar.backgroundColor = QBB_RED_COLOR;
    //返回按钮
    PublicBackBtn *backBtn = [[PublicBackBtn alloc]initWithVC:self andBackType:pop Img:[UIImage imageNamed:@"icon_back_white_press"]];
    [naviBar addSubview:backBtn];
    //标题
    UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 25)];
    titleLab.center = CGPointMake(WYMWITH/2, 42);
    titleLab.text = @"夺宝记录";
    titleLab.font = [UIFont systemFontOfSize:TITLE_SIZE];
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.textColor = [UIColor whiteColor];
    //分割线
    UILabel *fenge = [[UILabel alloc]initWithFrame:CGRectMake(0, 63.5, WYMWITH, 0.5)];
    fenge.backgroundColor = [UIColor lightGrayColor];
    [naviBar addSubview:fenge];
    [naviBar addSubview:titleLab];
    [self.view addSubview:naviBar];
}
//加载选项按钮
- (void)loadSelectBtns
{
    CGFloat btnW = WYMWITH/3;
    CGFloat btnH = 30;
    allBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    goBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rltBtn  = [UIButton buttonWithType:UIButtonTypeCustom];
   
    _btns = @[allBtn,goBtn,rltBtn];
    NSInteger indext = 0;
    NSArray *titles = @[@"全部",@"进行中",@"已揭晓"];
    for (UIButton *btn in _btns) {
        btn.frame = CGRectMake(btnW*indext, 64, btnW, btnH);
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        [btn setTitle:titles[indext] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [btn setTitleColor:QBB_RED_COLOR forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(btnsOnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
        indext++;
        btn.tag = indext;
    }
    allBtn.selected = YES;
    //跟随条
    _followLab = [[UILabel alloc]initWithFrame:CGRectMake(allBtn.frame.origin.x, CGRectGetMaxY(allBtn.frame), btnW, 2)];
    _followLab.backgroundColor = QBB_RED_COLOR;
    [self.view addSubview:_followLab];
}
//选项按钮的监听事件
- (void)btnsOnClick:(UIButton *)sender
{
    for (UIButton *btn in _btns) {
        btn.selected = NO;
    }
    sender.selected = YES;
    CGPoint oldPoint = _s.contentOffset;
    oldPoint.x = _s.frame.size.width*(sender.tag - 1);
    [UIView animateWithDuration:0.15 animations:^{
        _s.contentOffset = oldPoint;
        _followLab.frame = CGRectMake(sender.frame.origin.x, CGRectGetMaxY(sender.frame), _followLab.frame.size.width, 2);
    }];
}

//加载界面
- (void)loadSubViews
{
    _s = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 94, WYMWITH, WYMHEIGHT-94)];
    _s.pagingEnabled = YES;
    _s.delegate = self;
    for (NSInteger i = 0 ; i < _btns.count; i++) {
        DuoBaoHisTab *tab = [[DuoBaoHisTab alloc]initWithFrame:CGRectMake(WYMWITH * i, 2, WYMWITH,WYMHEIGHT - (20 + 44 + 30 + 2))];
        tab.tag = (i+1)*10;
        tab.father = self;
        tab.isMyRecordFlag = self.isMyRecordFlag;
        tab.userId = self.userId;
        [_s addSubview:tab];
    }
    _s.contentSize = CGSizeMake(_s.frame.size.width*3, 0);
    [self.view addSubview:_s];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger indext = scrollView.contentOffset.x/_s.frame.size.width;
    for (UIButton *btn in _btns) {
        btn.selected = NO;
    }
    UIButton *b = _btns[indext];
    b.selected = YES;
    _followLab.frame = CGRectMake(b.frame.origin.x, CGRectGetMaxY(b.frame), _followLab.frame.size.width, 2);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewWillDisappear:(BOOL)animated
{
    [WYPublic removeHudToWindow];
}

- (void)dealloc{
    
    NSLog(@"DuoDaohisVC dealloc");
}
@end
